using Microsoft.SPOT;
using Microsoft.SPOT.Presentation;
using Microsoft.SPOT.Presentation.Controls;
using Alarm;

namespace HouseControl
{
    class Door_Windows
    {
        
        public static void Door()
        {
               if (Program.inputPort.Read() == false) // we read the status of the port and give this status to the variable that represent the door
                    {
                        Program.gate = false;
                    }
               else
                    {
                        Program.gate = true;
                    }
        }


        public static void CheckingDoorPageIndex(object sender, EventArgs evt) // the index used to acess or to quit the Checking Door Page
        {
            if (Program.doorSet != null)  // if already exists then close it and return to the previous page
            {
                Program.doorSet.Close();
                Program.doorSet = null;
                Program.CheckingsPage();
            }
            else if (Program.doorSet == null) //if not then close the previous page and open the Page that we want
            {
                Program.mainWindow.Close();
                Program.mainWindow = null;
                DoorPage();
            }

        }

        public static void RefreshDoorPageIndex(object sender, EventArgs evt) //refresh the page 
        {
            if (Program.doorSet != null)
            {
                Program.doorSet.Close();
                Program.doorSet = null;
                DoorPage();
            }


        }

        public static Window DoorPage()  //window that display the status of the Door
        {

            Program.doorSet = new Window();
            Program.doorSet.Height = SystemMetrics.ScreenHeight;
            Program.doorSet.Width = SystemMetrics.ScreenWidth;
            StackPanel stackPanel = new StackPanel();

            Door_Windows.Door(); //check the status of the door 

          

                if (Program.gate == false)
                {

                    Image doorclose = new Image(Resources.GetBitmap(Resources.BitmapResources.doorclose1));
                    SimpleButton doorcloseButton = new SimpleButton(doorclose, Program.doorSet.Width, 74);

                    stackPanel.Children.Add(doorcloseButton);


                }
                else
                {
                    Image dooropen = new Image(Resources.GetBitmap(Resources.BitmapResources.dooropen1));
                    SimpleButton dooropenButton = new SimpleButton(dooropen, Program.doorSet.Width, 78);

                    stackPanel.Children.Add(dooropenButton);
                }

                Image refresh = new Image(Resources.GetBitmap(Resources.BitmapResources.refresh11));
                SimpleButton refreshButton = new SimpleButton(refresh, Program.doorSet.Width, 38);
                refreshButton.Click += new EventHandler(RefreshDoorPageIndex);

                stackPanel.Children.Add(refreshButton);

                Image back = new Image(Resources.GetBitmap(Resources.BitmapResources.back2));
                SimpleButton backButton = new SimpleButton(back, Program.doorSet.Width, 40);
                backButton.Click += new EventHandler(CheckingDoorPageIndex);

                stackPanel.Children.Add(backButton);
            




            Program.doorSet.Child = stackPanel;
            stackPanel.SetMargin(0, 0, 00, 0);
            Program.doorSet.Visibility = Visibility.Visible;


            return Program.doorSet;
        }
     
    }
}
